package com.aoxin.service.impl.course.systemCourse;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.aoxin.mapper.course.systemCourse.SchoolCourseDao;
import com.aoxin.mapper.course.systemCourse.SchoolCoursePageDao;
import com.aoxin.mapper.opm.SlfSchoolMapper;
import com.aoxin.model.course.AloneCoursePage;
import com.aoxin.model.course.AloneCourseToPage;
import com.aoxin.model.course.SchoolCourse;
import com.aoxin.model.course.SchoolCoursePage;
import com.aoxin.model.dto.SelectTreeDto;
import com.aoxin.model.opm.SlfEmployee;
import com.aoxin.model.opm.SlfSchool;
import com.aoxin.service.inter.course.systemCourse.AloneCoursePageService;
import com.aoxin.service.inter.course.systemCourse.AloneCourseToPageService;
import com.aoxin.service.inter.course.systemCourse.SchoolCoursePageService;
import com.aoxin.service.inter.course.systemCourse.SchoolCourseToPageService;
import com.aoxin.util.ErrorUtil;
import com.aoxin.util.ThreadLocalUtil;

@Service("SlfSchoolCoursePageService")
public class SchoolCoursePageServiceImpl implements SchoolCoursePageService {

	@Autowired
	private SchoolCoursePageDao schoolCoursePageDao;

	@Autowired
	private SchoolCourseDao schoolCourseDao;

	@Autowired
	private AloneCourseToPageService aloneCourseToPageService;

	@Autowired
	private AloneCoursePageService aloneCoursePageService;

	@Autowired
	private SchoolCourseToPageService schoolCourseToPageService;
	@Autowired
	private SlfSchoolMapper slfschoolmapper;

	/**
	 * 查询某个学校的课程包
	 * 
	 * @param schoolCoursePage
	 * @return
	 */
	@Override
	public List<SchoolCoursePage> getSchoolCoursePage(SchoolCoursePage schoolCoursePage) {
		List<SchoolCoursePage> schoolCoursePageList = schoolCoursePageDao.getSchoolCoursePage(schoolCoursePage);
		for (int i = 0,b=schoolCoursePageList.size(); i < b; i++) {
			SlfSchool slfSchool=	slfschoolmapper.selectByPrimaryKey(Integer.parseInt(schoolCoursePageList.get(i).getSchoolId()));
			schoolCoursePageList.get(i).setSchoolName(slfSchool.getName());
		}
		return schoolCoursePageList;
	}

	/**
	 * 新建某个学校的课程包
	 * 
	 * @param schoolCoursePage
	 * @return
	 */
	@Override
	@Transactional
	public void insertSchoolCoursePage(List<Integer> coursePageIds)  throws Exception{
		Assert.notEmpty(coursePageIds, String.format(ErrorUtil.PARAMETER_NULL, "coursePageIds"));
		for (Integer integer : coursePageIds) {
			AloneCoursePage aloneCoursePage = new AloneCoursePage();
			aloneCoursePage.setCoursePageId(integer);
			List<AloneCoursePage> aloneCoursePages = aloneCoursePageService.getAloneCoursePage(aloneCoursePage,1);
			if (aloneCoursePages.size() > 0) {
				aloneCoursePage = aloneCoursePages.get(0);
			}
			SchoolCoursePage schoolCoursePage = new SchoolCoursePage();
			BeanUtils.copyProperties(aloneCoursePage, schoolCoursePage);
			schoolCoursePage.setSchoolId(String.valueOf(ThreadLocalUtil.getEmployee().getSchoolId()));
			Integer count = schoolCoursePageDao.checkPage(ThreadLocalUtil.getEmployee().getSchoolId(), integer);
			if (count > 0) {
				  throw  new Exception(ErrorUtil.CHECK_PAKGE);
			} else {
				if (null == schoolCoursePage.getSequence()) {
					schoolCoursePage.setSequence(this.schoolCoursePageDao.getNextSequence(ThreadLocalUtil.getEmployee().getSchoolId()));
				}
				schoolCoursePageDao.insertSchoolCoursePage(schoolCoursePage);
			}
			AloneCourseToPage aloneCourseToPage = new AloneCourseToPage();
			aloneCourseToPage.setCoursePageId(integer);
			try {
				LinkedList<Map<String, Object>> list = this.aloneCourseToPageService.getCourseToPage(aloneCourseToPage);
				Integer[] schoolCourseIds = new Integer[list.size()];
				Integer schoolCourseid=ThreadLocalUtil.getEmployee().getSchoolId();
				for (int i = 0; i < list.size(); i++) {
					SchoolCourse schoolCourse = new SchoolCourse();
					schoolCourse.setSchoolId(schoolCourseid.toString());
					schoolCourse.setCourseId(Integer.valueOf(list.get(i).get("course_id").toString()));
					schoolCourseIds[i] = this.schoolCourseDao.getSchoolCourse(schoolCourse).get(0).getSchoolCourseId();
				}
				this.schoolCourseToPageService.insertSchoolCourseToPageDate(schoolCourseIds, schoolCoursePage.getSchoolCoursePageId());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 修改某个学校的课程包
	 * 
	 * @param schoolCoursePage
	 * @return
	 */
	@Override
	@Transactional
	public Integer updateSchoolCoursePage(SchoolCoursePage schoolCoursePage) throws Exception {
		 SlfEmployee employee = ThreadLocalUtil.getEmployee();
		   String schoolId=String.valueOf(employee.getSchoolId());
		   schoolCoursePage.setSchoolId(schoolId);
		   Assert.notNull(schoolId,ErrorUtil.SCHOOL_COURSE_SYSTEM);
		   //查询课程包库里的课程数据
		  SchoolCoursePage entity= schoolCoursePageDao.getAloneCoursePage(schoolCoursePage.getCoursePageId());
		  if(entity.getCoursePageId()!=null&&entity.getName()!=null){
			  entity.setSchoolId(schoolId);
			  entity.setCoursePageId(schoolCoursePage.getCoursePageId());
			  entity.setSchoolCoursePageId(schoolCoursePage.getSchoolCoursePageId());
			 Integer integer=schoolCoursePageDao.checkSchoolCoursePageinsert(schoolId, entity.getSchoolCoursePageId(), entity.getName());
			  if(integer==0){
				  return schoolCoursePageDao.updateSchoolCoursePage(entity);
			  }else{
				  throw  new Exception(ErrorUtil.CHECK_PAKGE);
			  }
		  }else{
			  throw new Exception(ErrorUtil.CHECK_ADD_TO_SCHOOL);
		  }
		
	}

	@Override
	public List<SelectTreeDto> getCoursePageTreeList() {
		List<SelectTreeDto> selectTreeDto = new ArrayList<SelectTreeDto>();
		selectTreeDto = this.schoolCoursePageDao.getCoursePageTreeList(null);
		coursePageTree(selectTreeDto);
		return selectTreeDto;
	}

	/**
	 * 递归课程包数
	 * 
	 * @param selectTreeDtos
	 */
	private void coursePageTree(List<SelectTreeDto> selectTreeDtos) {
		if (selectTreeDtos.size() > 0) {
			for (SelectTreeDto selectTreeDto : selectTreeDtos) {
				List<SelectTreeDto> treeDto = this.schoolCoursePageDao.getCoursePageTreeList(Integer.parseInt(selectTreeDto.getKey()));
				selectTreeDto.setChildren(treeDto);
				coursePageTree(treeDto);
			}
		}

	}

	@Override
	@Transactional
	public void setSchoolCoursePageTop(Integer schoolCoursePageId) {
		Assert.notNull(schoolCoursePageId, String.format(ErrorUtil.PARAMETER_NULL, "schoolCoursePageId"));
		Integer sequence = this.schoolCoursePageDao.getNextSequence(ThreadLocalUtil.getEmployee().getSchoolId());
		SchoolCoursePage schoolCoursePage = new SchoolCoursePage();
		schoolCoursePage.setSchoolCoursePageId(schoolCoursePageId);
		schoolCoursePage.setSequence(sequence);
		this.schoolCoursePageDao.updateSchoolCoursePage(schoolCoursePage);
	}

	@Override
	public List<SchoolCoursePage> getSchoolCoursePage(Integer syllabusId, String type) {
		Assert.notNull(syllabusId, String.format(ErrorUtil.PARAMETER_NULL, "syllabusId"));
		Assert.notNull(type, String.format(ErrorUtil.PARAMETER_NULL, "type"));
		if ("0".trim().equals(type)) {// 0表示传的是课表id
			return this.schoolCoursePageDao.getSchoolCoursePageBySyllabusId(syllabusId);
		} else {// 否则是课程id
			return this.schoolCoursePageDao.getSchoolCoursePageByCourseId(syllabusId);
		}

	}

	@Override
	public SchoolCoursePage getSchoolCoursePageById(Integer schoolCoursePageId) {
		return this.schoolCoursePageDao.getSchoolCoursePageById(schoolCoursePageId);
	}

}
